#!/usr/bin/env python
# coding: utf-8

# # Lesson-01 Assignment

#    各位同学大家好，欢迎各位开始学习我们的人工智能课程。这门课程假设大家不具备机器学习和人工智能的知识，但是希望大家具备初级的Python编程能力。根据往期同学的实际反馈，我们课程的完结之后 能力能够超过80%的计算机人工智能/深度学习方向的硕士生的能力。

# ![](https://img.kaikeba.com/web/kkb_index/img_index_logo.png)

# ## 本次作业的内容

# ### 1. Recode all examples;

# ### 2. Please answer some questions about our course. We do appreciate your help.

#    2.1  What do you want to get in this course? 

# Answer:I hope to learn the neccesary  professional knowleadge of CV to find a suitable job

# 2.2 What problems do you want to solve? 

# Answer:to solve pattern recognition and object detection

#  2.3 What advantages do you have to accomplish your goal?

# Answer:I have good math foundation and several years coding experience

#  2.4 What disadvantages you need to overcome to accomplish your goal?

# Answer:I have no deep fundation in AI

# 2.5 How will you plan to study in this course? 

# Answer:try my best to accomplish it

# ### 3. 如何提交

# 答疑平台提交，具体方式，见作业提交指南

# ### 4. 作业截止时间

# 作业能帮助你回顾课堂内容，你又可以通过作业进行代码实操。咱们可要认真、及时的完成作业哦！自布置作业起两周内提交，助教及时批改作业哦～逾期提交不批改。（特殊情况，请找班主任请假。）

# ### 5. 完成以下问答和编程练习

# 5.1  Please combine **image crop, color shift, rotation and perspective transform** together to complete a data augmentation script.
#    Your code need to be completed in Python/C++ in .py or .cpp file with comments and readme file to indicate how to use.

# In[3]:


import cv2
import random
import numpy as np
import matplotlib.pyplot as plt

def my_show(img,size=(2,2)):
    plt.figure(figsize=size)
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.show()

def image_crop(): # you code here
    img_ori = cv2.imread('/Users/liguanghui/Documents/技术教程/开课吧/1.5 图像处理基础/CV第一次上课代码和数据-课后作业/lenna.jpg', 1) 
    img_crop = img_ori[150:300,100:300]
    my_show(img_crop)
    

def img_cooler(img,b_increase,r_decrease):
    B,G,R = cv2.split(img)#对通道进行分割，并对每个通道进行处理
    b_lim = 255 - b_increase
    B[B > b_lim] = 255
    B[B <= b_lim] = (b_increase + B[B <= b_lim]).astype(img.dtype)
    r_lim = r_decrease
    R[R < r_lim] = 0
    R[R >= r_lim] = (R[R >= r_lim] - r_decrease).astype(img.dtype)
    return cv2.merge((B,G,R))    
    
def color_shift(): # you code here
    img_ori = cv2.imread('/Users/liguanghui/Documents/技术教程/开课吧/1.5 图像处理基础/CV第一次上课代码和数据-课后作业/lenna.jpg', 1) 
    img_cool = img_cooler(img_ori,50,10)
    my_show(img_cool)
        
        
def rotation(): # you code here
    img = cv2.imread('/Users/liguanghui/Documents/技术教程/开课吧/1.5 图像处理基础/CV第一次上课代码和数据-课后作业/lenna.jpg', 1) 
    cv2.imshow('src', img)
    imgInfo = img.shape
    height= imgInfo[0]
    width = imgInfo[1]
    deep = imgInfo[2]

    # 定义一个旋转矩阵
    matRotate = cv2.getRotationMatrix2D((height*0.5, width*0.5), 45, 0.7) # mat rotate 1 center 2 angle 3 缩放系数
    dst = cv2.warpAffine(img, matRotate, (height, width))
    cv2.imshow('image',dst)
    cv2.waitKey(0)

def perspective_transform(): # you code here
    pts1 = np.float32([[0,0],[0,500],[500,0],[500,500]])
    pts2 = np.float32([[5,19],[19,460],[460,9],[410,420]])
    #此时，有一个投影矩阵是从这两个点对之间变过来的
    M = cv2.getPerspectiveTransform(pts1,pts2)
    #print(M)
    #得到M之后，就可以用M对图片进行投影变换
    #参数分别是原图，M矩阵，长宽，
    img_ori = cv2.imread('/Users/liguanghui/Documents/技术教程/开课吧/1.5 图像处理基础/CV第一次上课代码和数据-课后作业/lenna.jpg', 1) 
    img_wrap = cv2.warpPerspective(img_ori,M,(500,500))
    my_show(img_wrap)

if __name__ == '__main__':
    image_crop()
    color_shift()
    rotation()
    perspective_transform()
    


# 各位同学，你已经把课上关于图像增广实现了！CV的领域很广，咱们需要思考兴趣点在哪～

# 5.2  Do think about your own interests very carefully and choose your topic within 3 weeks.

# Answer:object detection

# 这次的作业就到这里了！祝大家学习进步！

# ![image alt <](http://5b0988e595225.cdn.sohucs.com/images/20190420/1d1070881fd540db817b2a3bdd967f37.gif)
